package com.clouddisk.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clouddisk.common.core.domain.entity.RecoveryFile;
import com.clouddisk.common.core.domain.entity.UserFile;
import com.clouddisk.common.utils.FilesUtils;
import com.clouddisk.system.component.FileDealComp;
import com.clouddisk.system.domain.vo.file.RecoveryFileListVo;
import com.clouddisk.system.mapper.RecoveryFileMapper;
import com.clouddisk.system.mapper.UserFileMapper;
import com.clouddisk.system.service.IRecoveryFileService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class RecoveryFileServiceImpl extends ServiceImpl<RecoveryFileMapper, RecoveryFile> implements IRecoveryFileService {
    @Resource
    UserFileMapper userFileMapper;
    @Resource
    RecoveryFileMapper recoveryFileMapper;
    @Resource
    FileDealComp fileDealComp;


    @Override
    public void deleteUserFileByDeleteBatchNum(String deleteBatchNum) {


        LambdaQueryWrapper<UserFile> userFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userFileLambdaQueryWrapper.eq(UserFile::getDeleteBatchNum, deleteBatchNum);
        userFileMapper.delete(userFileLambdaQueryWrapper);



    }

    @Override
    public void restorefile(String deleteBatchNum, String filePath, Long sessionUserId, String fileDeptName) {
        List<UserFile> userFiles = userFileMapper.selectList(new LambdaQueryWrapper<UserFile>()
            .eq(UserFile::getDeleteBatchNum, deleteBatchNum)
            .eq(UserFile::getUserId,sessionUserId)
            .eq(UserFile::getFileDeptName,fileDeptName));
        userFiles.forEach(e -> fileDealComp.updateESByUserFileId(e.getUserFileId(),0));
        LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, 0)
                .set(UserFile::getDeleteBatchNum, "")
                .eq(UserFile::getDeleteBatchNum, deleteBatchNum);
        userFileMapper.update(null, userFileLambdaUpdateWrapper);
        FilesUtils filesUtils = new FilesUtils(filePath, true);
        fileDealComp.restoreParentFilePath(filesUtils, sessionUserId, fileDeptName);
        fileDealComp.deleteRepeatSubDirFile(filePath, sessionUserId, fileDeptName);
        // TODO 如果被还原的文件已存在，暂未实现

        LambdaQueryWrapper<RecoveryFile> recoveryFileServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        recoveryFileServiceLambdaQueryWrapper.eq(RecoveryFile::getDeleteBatchNum, deleteBatchNum);
        recoveryFileMapper.delete(recoveryFileServiceLambdaQueryWrapper);
    }

    @Override
    public List<RecoveryFileListVo> selectRecoveryFileList(Long userId, String fileDeptName) {
        return recoveryFileMapper.selectRecoveryFileList(userId, fileDeptName);
    }
}
